
#include <QSettings>
#include <QtDebug>

#include "tmsvrsettingsdlg.h"
//
CTmSvrSettingsDlg::CTmSvrSettingsDlg( QWidget * parent, Qt::WFlags f) 
	: QDialog(parent, f)
{
	setupUi(this);
	
	// add items to list box:
	new QListWidgetItem(tr("Network"), pageList);
	new QListWidgetItem(tr("Threads"), pageList);
	new QListWidgetItem(tr("Users"), pageList);
	new QListWidgetItem(tr("Pools"), pageList);
	
	// store current page index:
	m_nCurrentPageIndex = pageList->currentRow();
	
	// this is a bit cheeky - but as long as the list widgets are created in the same order as
	// the stacked pages, then we should be sweet.
	//
	// event better would eb to get the text for the list widgets from the stack pages themselves,
	// so adding a stack page in the UI designer would create a new item in the list widget
	// automagically!
	//connect(pageList, SIGNAL(currentRowChanged(int)), pageStack, SLOT(setCurrentIndex(int)));

	// get the reset, ok, apply and cancel buttons, so we can connect things up to them:
	QPushButton *pReset		= buttonBox->button(QDialogButtonBox::Reset);
	QPushButton *pOk 		= buttonBox->button(QDialogButtonBox::Ok);
	QPushButton *pApply 	= buttonBox->button(QDialogButtonBox::Apply);
	QPushButton *pCancel 	= buttonBox->button(QDialogButtonBox::Cancel);
	
	connect(pageList, SIGNAL(currentRowChanged(int)), SLOT(pageChanged(int)));
	connect((QObject*) pApply, SIGNAL(clicked()), SLOT(applySettings()));
	connect((QObject*) pReset, SIGNAL(clicked()), SLOT(resetSettings()));
	connect((QObject*) pOk, SIGNAL(clicked()), SLOT(accept()));
	connect((QObject*) pCancel, SIGNAL(clicked()), SLOT(reject()));

}
//

void CTmSvrSettingsDlg::loadSettings(int nPageNum)
{
	QSettings settings;
	
	QWidget *pageWidget = pageStack->widget(nPageNum);
	if (! pageWidget)
	{
		qWarning() << "Load: Could not find page widget for page index" << nPageNum;
	}
	
	QList<QWidget*> children = pageWidget->findChildren<QWidget*>(QRegExp("^_ed_.+"));
	
	while (!children.empty())
	{
		QWidget *pControl = children.takeFirst();
		
		QString strKey = QString("%1/%2").arg(pageWidget->objectName())
										 .arg(pControl->objectName().mid(4));
										 
		if (pControl->inherits("QSpinBox"))
		{
			QSpinBox *pSpinControl = static_cast<QSpinBox*>(pControl);
			
			pSpinControl->setValue(settings.value(strKey, pSpinControl->value()).toInt());
			qWarning() << "Loading:" << strKey << "=" << pSpinControl->value();
		}
		else if (pControl->inherits("QCheckBox"))
		{
			QAbstractButton *pButtonControl = static_cast<QAbstractButton*>(pControl);
			
			pButtonControl->setChecked(settings.value(strKey, pButtonControl->isChecked()).toBool());
			qWarning() << "Loading:" << strKey << "=" << pButtonControl->isChecked();
		}
	}
}

void CTmSvrSettingsDlg::saveSettings(int nPageNum)
{
	QSettings settings;
	
	QWidget *pageWidget = pageStack->widget(nPageNum);
	if (! pageWidget)
	{
		qWarning() << "Save: Could not find page widget for page index" << nPageNum;
		return;
	}
	
	// get all widget children that have a name that starts with "_ed_"
	QList<QWidget*> children = pageWidget->findChildren<QWidget*>(QRegExp("^_ed_.+"));
	
	while (!children.empty())
	{
		QWidget *pControl = children.takeFirst();
		
		// work out key:
		QString strKey = QString("%1/%2").arg(pageWidget->objectName())
										 .arg(pControl->objectName().mid(4));
		
		// now cast controlto appropriate type. This will have to be added to as we use more
		// and more simple controls.
		if (pControl->inherits("QSpinBox"))
		{
			QSpinBox *pSpinControl = static_cast<QSpinBox*>(pControl);
			
			settings.setValue(strKey, pSpinControl->value());
			qWarning() << "Saving:" << strKey << "=" << pSpinControl->value();
		}
		else if (pControl->inherits("QCheckBox"))
		{
			QAbstractButton *pButtonControl = static_cast<QAbstractButton*>(pControl);
			
			settings.setValue(strKey, pButtonControl->isChecked());
			qWarning() << "Saving:" << strKey << "=" << pButtonControl->isChecked();
		}
	}
}


void CTmSvrSettingsDlg::pageChanged(int nNewPage)
{
	qDebug() << "Changing page from" << m_nCurrentPageIndex << "to" << nNewPage;
	
	// load settings for new page we're about to show:
	loadSettings(nNewPage);
	pageStack->setCurrentIndex(nNewPage);
	m_nCurrentPageIndex = nNewPage;
}


bool CTmSvrSettingsDlg::settingsChanged(int nPageNum)
{
	return true;
}

void CTmSvrSettingsDlg::applySettings()
{
	if (settingsChanged(m_nCurrentPageIndex))
	{
		saveSettings(m_nCurrentPageIndex);
	}
}

void CTmSvrSettingsDlg::resetSettings()
{
	// re-load settings for current page:
	loadSettings(m_nCurrentPageIndex);
}
